<?php
/**
 * 功能权限管理
 */
apf_require_controller('Oa_Attendance_Abstract');
class Oa_Attendance_ManageController extends Oa_Attendance_AbstractController {
    const PAGEROW = 13;
    public function handle_request() {
        $bll_role = $this->init_role();

        //角色下拉框
        $role_select = $bll_role->get_all_role();
        $this->request->set_attribute('role_select',$role_select);

        $config = APF::get_instance()->get_config($config);
        $this->request->set_attribute('user_type',$config['user_type']);

        //城市下拉框
        $city_select = APF::get_instance()->get_config('oa_cities');
        $this->request->set_attribute('city_select',$city_select);

        $params = $this->request->get_parameters();

        if($params['action_user_type'] == 'set' && !empty($params['user_type']) && $params['set_role'] == 0 )
        {
            $rs = $this->set_users_to_type($params);
            $this->request->set_attribute('action_success',$rs ? '1':'0');
        }
        if($this->is_post_request()) {
            unset($params['p']);
        }
        $action = $params['action'];
        switch ($action) {
            case 'set':
                $rs = $this->set_role_to_users($params);
                $this->request->set_attribute('action_success',$rs ? '1':'0');
            default:
                $userlist = $this->search_users($params);
                $this->request->set_attribute('userlist',$userlist['data']);
            break;
        }
        if(!empty($userlist['total'])) {
            $url_params = array();//分页url参数
            if(!empty($params['search_city'])) {
                $url_params['search_city'] = $params['search_city'];
            }
            if(!empty($params['search_role'])) {
                $url_params['search_role'] = $params['search_role'];
            }
            if(!empty($params['search_keywords'])) {
                $url_params['search_keywords'] = $params['search_keywords'];
            }
            $page_num = $params['p'];
            $multipage = $this->build_multiPage_info($userlist['total'],$page_num,$url_params);
            $this->request->set_attribute('multipage',$multipage);
        }
        $this->request->set_attribute('params',$params);
        return 'Oa_Attendance_Manage';
    }

    /**
     * 获取翻页
     * @param unknown_type $totalnum 总的页数
     * @param unknown_type $page_num 当前第几页
     * @param array $url_params
     * @param int $uid 用户ID
     */
    public function build_multiPage_info($totalnum,$page_num = 0,$url_params = array()) {
        apf_require_class('Tools_MultiPage');
        //翻页信息
        $url = "/manage/";
        $total_rows = $totalnum;
        if($page_num <= 0){$page_num = 1;}
        $url_params['p'] = $page_num;

        $multipage = Tools_MultiPage::multiPage($url,$url_params, '', $total_rows, self::PAGEROW, 10);
        return $multipage;
    }

    /**
     * 设定选定用户的权限
     * @param unknown_type $params
     */
    public function set_role_to_users($params) {
        $result = false;
        $role_id = (int)$params['set_role'];
        if(empty($role_id) || empty($params['userlist'])) {
            return false;
        }

        $userlist = $params['userlist'];
        $bll_user = $this->init_user();
        $result = $bll_user->set_users_role($role_id,$userlist);
        return $result;
    }

    /**
     * 设定选定用户的特殊班别
     * @param unknown_type $params
     */
    public function set_users_to_type($params) {
        $result = false;
        $user_type = (int)$params['user_type'];
        if(empty($user_type) || empty($params['userlist'])) {
            return false;
        }

        $userlist = $params['userlist'];
        $bll_user = $this->init_user();
        $result = $bll_user->set_users_type($params['user_type'],$userlist);
        return $result;
    }

    /**
     * 搜索用户
     * @return array
     */
    public function search_users($params) {
        $where = array();
        if(!empty($params['search_city'])) {
            $where['city_name'] = $params['search_city'];
        }

        if(!empty($params['search_role'])) {
            $where['role_id'] = $params['search_role'];
        }

        if(!empty($params['search_keywords'])) {
            $words = $params['search_keywords'];
            $where['search_keywords'] = $params['search_keywords'];
            $where["(name like '{$words}%' or code = '{$words}') and 1=?"] = '1';
        }

        $page = (int)$params['p'];
        $page = empty($page) ? 1:$page;

        $order = "user_id asc";
        $offset = ($page-1)*self::PAGEROW;

        $bll_user = $this->init_user();
        $bll_user->enable_cache = false;
        $result = $bll_user->search_userinfo($where,$order,self::PAGEROW,$offset);
        $data = array();
        $data['data'] = $result['data'];
        $data['total'] = $result['total'];

        return $data;
    }
}